<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Constants\RobotApplyConstants;
use App\CustHub\Model\ChSmartAnswer;
use App\CustHub\Service\ChRobotApplyService;
use Mine\Abstracts\AbstractMapper;
use Hyperf\Config\Annotation\Value;
use Hyperf\Cache\Annotation\Cacheable;

class SmartAnswerMapper extends AbstractMapper
{
    #[Value('cache.default.prefix')]
    protected string $prefix;

    public function assignModel()
    {
        $this->model = ChSmartAnswer::class;
    }

    /**
     * 获取某个机器人下所有可执行方案
     * @param $robotId
     */
    #[Cacheable(prefix: "smart_answer_scheme", value:"#{robotId}", ttl:7200)]
    public function getSchemeList($robotId)
    {
        $ids=container()->get(ChRobotApplyService::class)->getApplyTargetOnlineIds(RobotApplyConstants::SMART_ANSWER,$robotId);
        if($ids->isEmpty()) return[];
        return $this->model::query()->whereIn('id',$ids)->where('status',1)->get()->toArray();
    }


    /**
     * 清除缓存
     * @param $robotId
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function clearSchemeCache($robotId): void
    {
        $redis = redis();
        $keyOne = $this->prefix . 'smart_answer_scheme:'.$robotId;
        $redis->del([$keyOne]);
    }



}